﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;

namespace RaceGame2013
{
    public class Kogel
    {
        // Content 
        ContentManager content;
        // Spritebatch
        SpriteBatch spriteBatch;


        //Set the sound effects to use
        SoundEffect gunShotSound;

        private Texture2D bulletTexture;
        private Vector2 bulletTarget;
        public Vector2 bulletPosition;
        private Vector2 bulletVelocity;
        public bool isActive;
        private float moveSpeed;
        public Rectangle bulletCollisionBox;
 
        public Kogel(ContentManager content, SpriteBatch spriteBatch)
        {
            isActive = false;

            // Reference to the contentenmanager
            this.content = content;

            // Referenc to the spriteBatch
            this.spriteBatch = spriteBatch;

            // Load a bullet texture
            bulletTexture = content.Load<Texture2D>("Kogel");

            // Load the sound effects
            gunShotSound = content.Load<SoundEffect>("Sound/Shot");
        }

        public void ActivateBullet(Vector2 point, Vector2 center)
        {
            bulletTarget = point;
            bulletPosition = center;
            moveSpeed = 200;
            isActive = true;
            bulletVelocity = bulletTarget;
            bulletVelocity.Normalize();
            
            gunShotSound.Play();
            //SetVelocity();
        }

        public void Update(GameTime gameTime)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (bulletPosition.Y < -30)
                isActive = false;
            if (bulletPosition.X < -30 || bulletPosition.X > 1500)
                isActive = false;
            bulletPosition += (bulletVelocity * moveSpeed * elapsedTime);
            bulletCollisionBox = new Rectangle((int)bulletPosition.X, (int)bulletPosition.Y, bulletTexture.Width, bulletTexture.Height);

        }

        public void Draw()
        {
            if (this.isActive == true)
            {
                spriteBatch.Draw(bulletTexture, bulletPosition, null, Color.White, 0f, new Vector2(bulletTexture.Width / 2, bulletTexture.Height / 2), 1.0f, SpriteEffects.None, 0f);
            }
        }

        public void Kill()
        {
            isActive = false;
        }
    }
}
